summaryrefslogtreecommitdiff
path: root/src/pages/[locale]/rss.xml.ts
blob: be2cab6a66d5f8e0852a042d3679e1a05a218225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import rss, { RSSFeedItem } from '@astrojs/rss';
import { CollectionEntry, getCollection } from 'astro:content';
import { getDate, getHref, isNewsItemInLocale } from '$lib/newsUtils';
import localeStaticPaths from '$lib/localeStaticPaths';
import getLocaleFromPath from '$lib/getLocaleFromPath';
import type { APIContext } from 'astro';
import translate from '$i18n/translate';
import tFeed from '$i18n/translations/pages/rss.xml';

export async function get(context: APIContext) {
  const locale = getLocaleFromPath(context.url.pathname);
  const t = translate(locale);
  const news = (await getCollection('news')).filter((item) => isNewsItemInLocale(item, locale));
  const site = context.site;
  if (!site) {
    throw new Error('No site provided in Astro configuration');
  }

  return rss({
    title: t(tFeed, { key: 'title' }),
    description: t(tFeed, { key: 'description' }),
    site,
    items: news.map(newsItemToRssFeedItem),
    customData: `<language>${locale}</language>`,
  });
}

function newsItemToRssFeedItem(item: CollectionEntry<'news'>): RSSFeedItem {
  const pubDate = getDate(item);
  if (!pubDate) {
    throw new Error(`Could not determine publication date of ${item}`);
  }

  return {
    link: getHref(item),
    title: item.data.title,
    description: item.data.summary,
    pubDate,
  };
}

export async function getStaticPaths() {
  return localeStaticPaths;
}